use serde::{Deserialize, Serialize};
use serde_with::base64::Base64;
use validator::Validate;
use wanmesh_common::validator::validate_string_is_not_empty;

#[derive(Serialize, Deserialize, Validate, Debug, Clone)]
pub struct LoginApplyRequest {
    #[validate(custom(function = "validate_string_is_not_empty"))]
    pub username: String,
}

#[serde_with::serde_as]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LoginResponseData {
    pub apply_id: String,
    #[serde_as(as = "Base64")]
    pub public_key: Vec<u8>,
}

#[serde_with::serde_as]
#[derive(Serialize, Deserialize, Validate, Debug, Clone)]
pub struct LoginSubmitRequest {
    #[validate(custom(function = "validate_string_is_not_empty"))]
    pub apply_id: String,
    #[validate(custom(function = "validate_string_is_not_empty"))]
    pub username: String,
    #[serde_as(as = "Base64")]
    pub password_crypto: Vec<u8>,
}

#[derive(Serialize, Deserialize, Validate, Debug, Clone)]
pub struct RefreshTokenRequest {
    #[validate(custom(function = "validate_string_is_not_empty"))]
    pub refresh_token: String,
}

impl From<String> for RefreshTokenRequest {
    fn from(refresh_token: String) -> Self {
        Self { refresh_token }
    }
}

impl From<&str> for RefreshTokenRequest {
    fn from(refresh_token: &str) -> Self {
        Self {
            refresh_token: refresh_token.to_string(),
        }
    }
}
